package com.hbt.yiqing.fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.yiqing.R;
import com.hbt.yiqing.adapter.RecChinaAdapter;
import com.hbt.yiqing.entity.YQInfo;
import com.hbt.yiqing.utils.DataUtils_world;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class AfricaFragment extends Fragment implements OnClickListener {

    RecyclerView recycle;
    private String c_name = "非洲";


    //    @BindView(R.id.confirm_sort)
    TextView confirmSort;
    //    @BindView(R.id.curConfirm_sort)
    TextView curConfirmSort;
    //    @BindView(R.id.heal_sort)
    TextView healSort;
    //    @BindView(R.id.dead_sort)
    TextView deadSort;

    boolean confirm_asc = true;
    boolean cur_confirm_asc= true;
    boolean suspect_asc = true;
    boolean heal_asc = true;
    boolean dead_asc = true;
    public AfricaFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_asian, container, false);
        Log.i("6678", c_name);
        recycle = v.findViewById(R.id.recycle);
        initRecycler();
        onViewClicked(v);
        return v;
    }


    public void initRecycler() {
        List<YQInfo> nations = null;
        while (nations == null) {
            nations = DataUtils_world.getNationsByContinents("非洲");
//            System.out.println("nations null");
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(AfricaFragment.this.getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle.setLayoutManager(layoutManager);
        recycle.setAdapter(new RecChinaAdapter(nations));

    }//initRecycler end

    public void onViewClicked(View view) {
        confirmSort = view.findViewById(R.id.confirm_sort);
        curConfirmSort = view.findViewById(R.id.curConfirm_sort);
        healSort = view.findViewById(R.id.heal_sort);
        deadSort = view.findViewById(R.id.dead_sort);

        confirmSort.setOnClickListener(this);
        curConfirmSort.setOnClickListener(this);
        healSort.setOnClickListener(this);
        deadSort.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        RecChinaAdapter recChinaAdapter = ((RecChinaAdapter) recycle.getAdapter());
        List<YQInfo> yqInfoss = recChinaAdapter.getInfos();
        switch (v.getId()) {
            case R.id.confirm_sort:
                confirm_asc = confirm_asc?false:true;
                Collections.sort(yqInfoss, new Comparator<YQInfo>() {
                    @Override
                    public int compare(YQInfo o1, YQInfo o2) {
                        return confirm_asc?o1.getConfirm()-o2.getConfirm():o2.getConfirm()-o1.getConfirm();
                    }
                });
                break;
            case R.id.curConfirm_sort:
                cur_confirm_asc = cur_confirm_asc?false:true;
                Collections.sort(yqInfoss, new Comparator<YQInfo>() {
                    @Override
                    public int compare(YQInfo o1, YQInfo o2) {
                        return cur_confirm_asc?o1.getNowConfirm()-o2.getNowConfirm():o2.getNowConfirm()-o1.getNowConfirm();
                    }
                });
                break;
            case R.id.heal_sort:
                heal_asc = heal_asc?false:true;
                Collections.sort(yqInfoss, new Comparator<YQInfo>() {
                    @Override
                    public int compare(YQInfo o1, YQInfo o2) {
                        return heal_asc?o1.getHeal()-o2.getHeal():o2.getHeal()-o1.getHeal();
                    }
                });
                break;
            case R.id.dead_sort:
                dead_asc = dead_asc?false:true;
                Collections.sort(yqInfoss, new Comparator<YQInfo>() {
                    @Override
                    public int compare(YQInfo o1, YQInfo o2) {
                        return dead_asc?o1.getDead()-o2.getDead():o2.getDead()-o1.getDead();
                    }
                });
                break;
        }//switch end
        recChinaAdapter.notifyDataSetChanged();
    }//onClick end

}
